/*! \file mmc.c \brief MultiMedia and SD Flash Card Interface. */
//*****************************************************************************
//
// File Name	: 'mmc.c'
// Title		: MultiMedia and SD Flash Card Interface
// Author		: Pascal Stang - Copyright (C) 2004
// Created		: 2004.09.22
// Revised		: 2004.09.22
// Version		: 0.1
// Target MCU	: Atmel AVR Series
// Editor Tabs	: 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested.  Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

//----- Include Files ---------------------------------------------------------
#include <avr/io.h>			// include I/O definitions (port names, pin names, etc)
//#include <avr/signal.h>		// include "signal" names (interrupt names)
#include <avr/interrupt.h>	// include interrupt support

#include "globals.h"		// include our global settings
#include "spi.h"		// include spi bus support


#define SD_APP_OP_COND	41
#define SD_INIT			40


#include "mmc.h"

// include project-specific hardware configuration
#include "mmcconf.h"

// Global variables

// Functions

uint8_t mmcInit(void)
{
	uint8_t rtn;

	// initialize SPI interface
	spiInit(0);
	// release chip select
	sbi(MMC_CS_DDR, MMC_CS_PIN);
	sbi(MMC_CS_PORT,MMC_CS_PIN);
	cbi(MMC_CS_PORT,MMC_CS_PIN);

	if ((rtn=mmcReset()))
		spiInit(0);

	sbi(MMC_CS_PORT,MMC_CS_PIN);

	return rtn;
}

uint8_t mmcReset(void)
{
	u16 retry;
	uint8_t r1=0;
	uint8_t data[512];

	for (retry=0;retry<512;retry++)
	{
		data[retry] = retry;
	}

	retry = 40;

	cbi(MMC_CS_PORT,MMC_CS_PIN);

	for(r1=0; r1 < 80; r1++) spiTransferByte(0xFF); 

	while (--retry)
	{
		//spiTransferByte(0x00);
		spiTransferByte(0x40);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0x95);
		spiTransferByte(0xFF);
		if (spiTransferByte(0xFF) == 0x01)
			break;
	};

	if ( !retry )
		return 0;
	
	/*retry = 1000;
	while (--retry)
	{
		//spiTransferByte(0x01);
		spiTransferByte(0x41);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0x00);
		spiTransferByte(0xFF);
		spiTransferByte(0xFF);
		if (spiTransferByte(0xFF) == 0x01)
			break;
	};*/

	if ( !retry )
	{
		if ( !mmcCommand(55, 0) & 0x01)
			return 2;
		
		retry = 1000;
		while (--retry)
		{
			if (mmcCommand(SD_APP_OP_COND, 0) & 0x01)
				break;
		}

		if ( !retry )
			return 3;
	}

	if (mmcCommand(MMC_TAG_SECTOR_START, 0) != 0x01
	|| mmcCommand(MMC_TAG_SECTOR_END, 5120) != 0x01
	|| mmcCommand(MMC_ERASE,0) != 0x01)
	{
		return 4;
	}

	mmcWrite(0,data);

	for (retry=0;retry<512;retry++)
	{
		data[retry] = 0;
	}

	mmcRead(0, data);

	for (retry=0;retry<512;retry++)
	{
		if (data[retry] != (retry & 0xFF))
			break;
	}


	return mmcCommand(MMC_SEND_STATUS, 0);

}

uint8_t mmcSendCommand(uint8_t cmd, u32 arg)
{
	uint8_t r1;

	// assert chip select
	cbi(MMC_CS_PORT,MMC_CS_PIN);
	// issue the command
	r1 = mmcCommand(cmd, arg);
	// release chip select
	sbi(MMC_CS_PORT,MMC_CS_PIN);

	return r1;
}

uint8_t mmcRead(u32 sector, uint8_t* buffer)
{
	uint8_t r1;
	u16 i;

	// assert chip select
	cbi(MMC_CS_PORT,MMC_CS_PIN);
	// issue command
	r1 = mmcCommand(MMC_READ_SINGLE_BLOCK, sector<<9);
	#ifdef MMC_DEBUG
	rprintf("MMC Read Block R1=0x%x\r\n", r1);
	#endif
	// check for valid response
	if(r1 != 0x00)
		return r1;
	// wait for block start
	while(spiTransferByte(0xFF) != MMC_STARTBLOCK_READ);
	// read in data
	for(i=0; i<0x200; i++)
	{
		*buffer++ = spiTransferByte(0xFF);
	}
	// read 16-bit CRC
	spiTransferByte(0xFF);
	spiTransferByte(0xFF);
	// release chip select
	sbi(MMC_CS_PORT,MMC_CS_PIN);
	// return success
	return 0;
}

uint8_t mmcWrite(u32 sector, uint8_t* buffer)
{
	uint8_t r1;
	u16 i;

	// assert chip select
	cbi(MMC_CS_PORT,MMC_CS_PIN);
	// issue command
	r1 = mmcCommand(MMC_WRITE_BLOCK, sector<<9);
	#ifdef MMC_DEBUG
	rprintf("MMC Write Block R1=0x%x\r\n", r1);
	#endif
	// check for valid response
	if(r1 != 0x00)
		return r1;
	// send dummy
	spiTransferByte(0xFF);
	// send data start token
	spiTransferByte(MMC_STARTBLOCK_WRITE);
	// write data
	for(i=0; i<0x200; i++)
	{
		spiTransferByte(*buffer++);
	}
	// write 16-bit CRC (dummy values)
	spiTransferByte(0xFF);
	spiTransferByte(0xFF);
	// read data response token
	r1 = spiTransferByte(0xFF);
	if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
		return r1;
	#ifdef MMC_DEBUG
	rprintf("Data Response Token=0x%x\r\n", r1);
	#endif
	// wait until card not busy
	while(!spiTransferByte(0xFF));
	// release chip select
	sbi(MMC_CS_PORT,MMC_CS_PIN);
	// return success
	return 0;
}



uint8_t mmcCommand(uint8_t cmd, u32 arg)
{
	uint8_t r1;
	register uint8_t retry=10;
	// send command
	spiTransferByte(0xFF);
	spiTransferByte(cmd | 40);
	spiTransferByte(arg>>24);
	spiTransferByte(arg>>16);
	spiTransferByte(arg>>8);
	spiTransferByte(arg);
	spiTransferByte(0x95);	// crc valid only for MMC_GO_IDLE_STATE
	spiTransferByte(0xFF);
	// end command
	// wait for response
	// if more than 8 retries, card has timed-out
	// return the received 0xFF
	while (((r1 = spiTransferByte(0xFF)) == 0xFF) && --retry);
		//if(retry++ > 20000) break;
	// return response
	return r1;
}
